package com.example.ymm.modules1.controller.user;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.*;
import com.example.ymm.modules1.query.DbUserRewardsQueryCriteria;
import com.example.ymm.modules1.service.dto.*;
import com.example.ymm.modules1.service.user.*;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.UtilValidate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/hl/user/info")
@Tag(name= "用户端 书本信息接口")
@Slf4j
@CrossOrigin
public class UserInfoController extends BaseController {

    @Autowired
    DbUserService dbUserService;

    @Autowired
    DbClazzLevelService dbClazzLevelService;
    @Autowired
    DbClazzService dbClazzService;
    @Autowired
    DbUserRewardsService dbUserRewardsService;
    @Autowired
    DbIntegralLogService dbIntegralLogService;
    @Autowired
    DbCardService dbCardService;

    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    @Operation(summary= "获取登录者信息")
    public CommonResult getUserInfo() {
        try {
            String userId = getUserId();
            DbUser user = dbUserService.getById(userId);
            if(UtilValidate.isNotEmpty(user.getLevelName())){
                DbClazzLevelQueryCriteria criteria=new DbClazzLevelQueryCriteria();
                criteria.setLevelName(user.getLevelName());
                List<DbClazzLevel> list = dbClazzLevelService.queryAll(criteria);
                if(UtilValidate.isNotEmpty(list)){
                    DbClazzLevel clazzLevel=list.get(0);
                    String clazzId = clazzLevel.getClazzId();
                    DbClazz clazz = dbClazzService.getById(clazzId);
                    user.setClazzName(clazz.getClazzName());
                }else{
                    user.setClazzName("基层启航班");
                    user.setLevelName("C1");
                }
            }
            DbIntegralLogQueryCriteria c1=new DbIntegralLogQueryCriteria();
            c1.setUserId(userId);
//            Integer integralAll = dbIntegralLogService.countUserIntegral(c1);
//            user.setIntegralAll(integralAll);
            DbIntegralLogQueryCriteria c2=new DbIntegralLogQueryCriteria();
            c2.setUserId(userId);
            c2.setType(2);
            //消耗积分计算
            Integer integralExpend = dbIntegralLogService.countUserIntegral(c2);
            if(UtilValidate.isEmpty(integralExpend)){
                integralExpend=0;
            }
            user.setIntegralExpend(integralExpend);
            //总积分
            Integer integral = user.getIntegral();
            Integer integralAll=0+integral;
            if(UtilValidate.isNotEmpty(integralExpend)){
                integralAll+=integralExpend;
            }
            user.setIntegralAll(integralAll);

            //读书积分
            SysRankingQueryCriteria sysQueryCriteria=new SysRankingQueryCriteria();
            sysQueryCriteria.setUserId(userId);
            SysRankingListDto userReadIntegral = dbIntegralLogService.getUserReadIntegral(sysQueryCriteria);
            Integer integralRead=0;
            if(UtilValidate.isNotEmpty(userReadIntegral)){
                integralRead=userReadIntegral.getNum();
            }
            user.setIntegralRead(integralRead);

            //月和年 满勤情况
            Date now = new Date();
            String month = DateKit.dateFormat(now, "yyyy-MM");
            String year = DateKit.dateFormat(now, "yyyy");
            Integer monthCardType=0;
            DbCardQueryCriteria cardQuery1=new DbCardQueryCriteria();
            cardQuery1.setUserId(userId);
            cardQuery1.setIsFullMonth(1);
            cardQuery1.setMonth(month);
            List<DbCard> dbCards = dbCardService.queryAll(cardQuery1);
            if(UtilValidate.isNotEmpty(dbCards)){
                monthCardType=1;
            }
            DbCardQueryCriteria cardQuery2=new DbCardQueryCriteria();
            cardQuery2.setUserId(userId);
            cardQuery2.setIsAllMonth(1);
            cardQuery2.setMonth(month);
            List<DbCard> dbCards2 = dbCardService.queryAll(cardQuery2);
            if(UtilValidate.isNotEmpty(dbCards2)){
                monthCardType=2;
            }

            Integer yearCardType=0;
            DbCardQueryCriteria cardQuery3=new DbCardQueryCriteria();
            cardQuery3.setUserId(userId);
            cardQuery3.setIsFullYear(1);
            cardQuery3.setMonth(year);
            List<DbCard> dbCards3 = dbCardService.queryAll(cardQuery3);
            if(UtilValidate.isNotEmpty(dbCards3)){
                yearCardType=1;
            }
            DbCardQueryCriteria cardQuery4=new DbCardQueryCriteria();
            cardQuery4.setUserId(userId);
            cardQuery4.setIsAllYear(1);
            cardQuery4.setMonth(year);
            List<DbCard> dbCards4 = dbCardService.queryAll(cardQuery4);
            if(UtilValidate.isNotEmpty(dbCards4)){
                yearCardType=2;
            }
            user.setMonthCardType(monthCardType);
            user.setYearCardType(yearCardType);
            return CommonResult.success(user);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }


    }

    @RequestMapping(value = "/getIsLogin", method = RequestMethod.POST)
    @Operation(summary= "是否登录")
    public CommonResult getIsLogin() {
        try {
            String userId = getUserId();
            return CommonResult.success(userId);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }


    }


    @Operation(summary= "用户奖惩记录列表")
    @RequestMapping(value = "/userRewardsList", method = RequestMethod.POST)
    public CommonResult userRewardsList(@Valid DbUserRewardsQueryCriteria criteria, YmPageDto ymPage) {
        try{
            String userId = getUserId();
            criteria.setUserId(userId);
            TableList<DbUserRewards> list = dbUserRewardsService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "获取用户已打卡列表")
    @RequestMapping(value = "/getUserCards", method = RequestMethod.POST)
    public CommonResult getUserCards(DbCardQueryCriteria criteria, YmPageDto ymPage) {
        try{
            String userId = getUserId();
            if(UtilValidate.isEmpty(criteria.getUserId())){
                criteria.setUserId(userId);
            }
            TableList<DbCard> list = dbCardService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "用户 年分钟数列表")
    @RequestMapping(value = "/userYearCountList", method = RequestMethod.POST)
    public CommonResult userYearCountList(@Valid DbCardQueryCriteria criteria, YmPageDto ymPage) {
        try{
            String userId = getUserId();
            criteria.setUserId(userId);
            TableList<UserTimeCountDto> list = dbCardService.getUserYearCount(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "用户 月分钟数列表")
    @RequestMapping(value = "/userMonthCountList", method = RequestMethod.POST)
    public CommonResult userMonthCountList(@Valid DbCardQueryCriteria criteria, YmPageDto ymPage) {
        try{
            String userId = getUserId();
            criteria.setUserId(userId);
            TableList<UserTimeCountDto> list = dbCardService.getUserMonthCount(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

}
